{% load i18n %}
<script>
    function iommi_table_js_select_all(base, has_paginator) {
        var table = base.closest('table');
        var tbody = table.querySelector('tbody');
        // Select all checkboxes on this page
        Array.prototype.forEach.call(tbody.querySelectorAll('.checkbox'), function(el, i) {
            el.click();
        });

        // If there are multiple pages
        if (has_paginator) {
            // If we haven't done so already offer to select everything
            if (tbody.querySelector('.select_all_pages_q') === null) {
                tbody.querySelector('tr').insertAdjacentHTML('beforebegin', '<tr><td colspan="99" style="text-align: center" class="select_all_pages_q">{% trans "All items on this page are selected." %} <a onclick="iommi_table_js_select_all_pages(this)" href="#">{% trans "Select all items" %}</a></td></tr>'
                )
            }
            else {
                // Otherwise the select all button was hit again (and nothing should be selected)
                // Hide the select everything again.
                var row_with_select_all = tbody.querySelector('.select_all_pages_q').closest('tr');
                row_with_select_all.parentNode.removeChild(row_with_select_all);
                var form = base.closest('form');
                form.querySelector('.all_pks').value = 0;
            }
        }
    }

    function iommi_table_js_select_all_pages(base) {
        var form = base.closest('form');
        var tbody = form.querySelector('tbody');
        tbody.querySelector('.select_all_pages_q').textContent = '{% trans "All items selected" %}';
        form.querySelector('.all_pks').value = 1;
    }
</script>
